/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import java.sql.*;
import Module.*;

/**
 *
 * @author Vegard
 */
public class DecisionListController {

    Database database = new Database();
    public boolean registerDecisionList(DecisionList decisionList) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());

            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into decisionList values(DEFAULT,?,?,?,?)", autogen);
            statement.setInt(1, decisionList.getSessionId());
            statement.setString(2, decisionList.getDescription());
            statement.setString(3, decisionList.getStatus());
            statement.setString(4, decisionList.getResponsible());
            statement.executeUpdate();
            conn.close();
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    public boolean editDecisionList(DecisionList decisionList) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());

            PreparedStatement statement = conn.prepareStatement(
                    "update decisionList set sessionId=?, description=?, status=?,responsible=? "
                    + "where decisionListId=?");
            statement.setInt(1, decisionList.getSessionId());
            statement.setString(2, decisionList.getDescription());
            statement.setString(3, decisionList.getStatus());
            statement.setString(4, decisionList.getResponsible());
            statement.setInt(5, decisionList.getId());
            statement.executeUpdate();
            conn.close();
            return true;

        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    public boolean deletDecisionList(DecisionList decisionList){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from decisionList where decisionListId=?");
            statement.setInt(1, decisionList.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }

        return false;
    }

    public DecisionList getDecisionListToSession(Session session){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from decisionList where sessionId=?");
            statement.setInt(1, session.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                DecisionList dl = new DecisionList(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5));
                conn.close();
                return dl;
            }

        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
}
